package models

import (
	"strings"
	"time"
)

//HookUser gitee user model
type HookUser struct {
	Id        int64
	Login     string //Same as username
	Name      string //User's nickname
	Email     string //User's mailbox
	UserName  string `json:"username"`   //User's code cloud personal space address
	AvatarUrl string `json:"avatar_url"` //profile picture
	SiteAdmin bool   `json:"site_admin"` //Is it an administrator

}

//HookIssue gitee issue model
type HookIssue struct {
	Id           int64
	Number       string
	Title        string
	Labels       []IssueLabel `json:"labels"`
	State        string       `json:"state"`
	HtmlUrl      string       `json:"html_url"` //Comment on the url on the code cloud
	Body         string       `json:"body"`
	StateName    string       `json:"state_name"`
	User         HookUser
	Assignee     HookUser
	Repository   HookRepository
	CreateAt     time.Time `json:"created_at"`
	UpdateAt     time.Time `json:"updated_at"`
	FinishedAt   time.Time `json:"finished_at"`
	IssueType    string    `json:"issue_type"`
	TypeName     string    `json:"type_name"`
	SecurityHole bool      `json:"security_hole"`
	IssueState   string    `json:"issue_state"`
}

//IssueLabel issue label
type IssueLabel struct {
	Id    int64  `json:"id"`
	Name  string `json:"name"`
	Color string `json:"color"`
}

//HookRepository gitee repository model
type HookRepository struct {
	Id       int64
	FullName string `json:"full_name"`
	Url      string `json:"url"`
	Path     string `json:"path"`
	Name     string
	Owner    HookUser
	Private  bool
	public   bool
}

//HookIssueRepository gitee repository model
type HookIssueRepository struct {
	Id        int64
	FullName  string `json:"full_name"`
	Url       string `json:"url"`
	Path      string `json:"path"`
	NameSpace string `json:"namespace"`
	Name      string
	Owner     HookUser
	Private   bool
	public    bool
}

//GitIssue gitee issue model
type GitIssue struct {
	Id           int64
	Number       string
	Title        string
	Labels       []IssueLabel `json:"labels"`
	State        string       `json:"state"`
	HtmlUrl      string       `json:"html_url"` //Comment on the url on the code cloud
	Body         string       `json:"body"`
	StateName    string       `json:"state_name"`
	User         HookUser
	Assignee     HookUser
	Repository   Repository
	CreateAt     time.Time `json:"created_at"`
	UpdateAt     time.Time `json:"updated_at"`
	FinishedAt   time.Time `json:"finished_at"`
	IssueType    string    `json:"issue_type"`
	TypeName     string    `json:"type_name"`
	SecurityHole bool      `json:"security_hole"`
	IssueState   string    `json:"issue_state"`
}

//Repository gitee repository model
type Repository struct {
	Id        int64
	FullName  string `json:"full_name"`
	Url       string `json:"url"`
	Path      string `json:"path"`
	Name      string
	NameSpace NameSpace `json:"namespace"`
	Owner     HookUser
	Private   bool
	Public    bool
}

type NameSpace struct {
	Id   int64
	Path string `json:"path"`
	Name string `json:"name"`
}

//HookComment gitee comment model
type HookComment struct {
	User     *HookUser //Comment author information
	HtmlUrl  string    `json:"html_url"` //Comment on the url on the code cloud
	Id       int64
	Body     string    `json:"body"` //Comment content
	CreateAt time.Time `json:"created_at"`
	UpdateAt time.Time `json:"updated_at"`
}

//CommentPayload hook data triggered by a comment task operation
type CommentPayload struct {
	Action        string // Action comment
	HookName      string `json:"hook_name"`
	Password      string `json:"password"` //The password of the hook
	HookId        int64  `json:"hook_id"`  //Hook ID
	HookUrl       string `json:"hook_url"` //Hook routing
	Timestamp     string
	Sign          string       //The signature calculated by the hook based on the key
	Comment       *HookComment `json:"comment"` //Comment data
	Note          string       //Comment data of the commented target
	NoteableType  string       `json:"noteable_type"` //Target type being reviewed
	NoteableId    int64        `json:"noteable_id"`   //ID of the target being reviewed
	Title         string       //Target title being commented
	PerId         string       `json:"per_iid"`         //Target ID being reviewed
	ShortCommitId string       `json:"short_commit_id"` //The short sha in the commented commit submission
	Issue         *HookIssue
}

type IssuePayload struct {
	HookId      int64     `json:"hook_id"`   //  Hook id.
	HookUrl     string    `json:"hook_url"`  // route
	HookName    string    `json:"hook_name"` // issue_hooks。
	Password    string    `json:"password"`  // Hook code
	Action      string    //issue status
	Issue       HookIssue //issue information
	Sender      HookUser  //The user information that triggered the hook.
	TargetUser  HookUser  `json:"target_user"` //User information entrusted to handle the issue
	User        HookUser  //issue creator
	Assignee    HookUser  //issue principal
	Iid         string    //issue Logo
	Title       string    //issue title
	Description string    //issue description
	State       string    //issue status
	Url         string    //issue URL on code cloud
	Repository  HookIssueRepository
}

type PullRequest struct {
	Id        int64
	Number    int
	State     string
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	ClosedAt  time.Time `json:"closed_at"`
	MergedAt  time.Time `json:"merged_at"`
}

type PullRequestIssue struct {
	Id        int64
	Number    string
	Repo      string
	CveNumber string
	Branch    string
	BrFlag    bool
}

//GetLabelsStr labels slice to string
func (ih *HookIssue) GetLabelsStr() (labels string) {
	var lb []string
	for _, v := range ih.Labels {
		lb = append(lb, v.Name)
	}
	return strings.Join(lb, ",")
}

//ReplaceLabelToStr delete old label and add new label and return label string
func (ih HookIssue) ReplaceLabelToStr(oLabel, nLabel string) (labels string) {
	var lb []string
	es := false
	for _, v := range ih.Labels {
		if v.Name == nLabel {
			es = true
		}
		if v.Name != oLabel {
			lb = append(lb, v.Name)
		}
	}
	if !es {
		lb = append(lb, nLabel)
	}
	return strings.Join(lb, ",")
}
